package com.situ.springboot.controller;

import com.situ.springboot.pojo.Blog;
import com.situ.springboot.pojo.query.BlogQuery;
import com.situ.springboot.service.IBlogService;
import com.situ.springboot.util.LayUITableResult;
import com.situ.springboot.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//注解
@Controller
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private IBlogService blogService;

    @RequestMapping("/toAdd")
    public String toAdd() {
        return "blog_add";
    }

    //{'name':'zhansgan','password':'123','email':'123'}
    @RequestMapping("/add")
    @ResponseBody
    public Result add(Blog blog) {
        blogService.add(blog);
        return Result.ok("添加成功");
    }

    // /blog/deleteAll?ids=1,2,3
    @RequestMapping("/deleteAll")
    @ResponseBody
    public Result deleteAll(Integer[] ids) {
        blogService.deleteAll(ids);
        return Result.ok("删除成功");
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public Result deleteById(Integer id) {
        blogService.deleteById(id);
        return Result.ok("删除成功");
    }

    @RequestMapping("/toList")
    public String toList() {
        return "blog_list";
    }

    // /blog/list?page=1&limit=10&name=zhang&email=3fdd
    @RequestMapping("/list")
    @ResponseBody
    public LayUITableResult list(BlogQuery blogQuery) {
        LayUITableResult layUITableResult = blogService.list(blogQuery);
        return layUITableResult;
    }

    //根据id删除
    public void deleteById() {

    }

    /*
    @RequestMapping("/list")
    public String list(Model model) {
        List<Blog> list = blogService.list();
        model.addAttribute("list", list);
        //代表转发到blog_list.html这个页面
        return "blog_list";
    }

    // [{'id':1,'name':'zhangsan'},{}]
    // http://127.0.0.1:8080/blog/list
    // http://localhost:8080/blog/list
    @RequestMapping("/list1")
    @ResponseBody  //返回json格式数据
    public List<Blog> list1() {
        *//*Blog blog1 = new Blog();
        blog1.setId(1);
        blog1.setName("zhangsan");
        blog1.setAge(23);
        Blog blog2 = new Blog();
        blog2.setId(2);
        blog2.setName("lisi");
        blog2.setAge(24);
        List<Blog> list = new ArrayList<>();
        list.add(blog1);
        list.add(blog2);*//*

        List<Blog> list = blogService.list();
        return list;
    }*/
}
